﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

using mvcForum.Web.ViewModels;

namespace mvcForum.Web.Extensions {

	public static class GroupExtensions {

		/// <summary>
		/// Method that takes a collection of group view models and creates a matching collection of select list items.
		/// </summary>
		/// <param name="groups">The collection of group view models.</param>
		/// <returns></returns>
		public static IEnumerable<SelectListItem> ToSelectList(this IEnumerable<GroupViewModel> groups) {
			return groups.ToSelectList(null);
		}

		/// <summary>
		/// Method that takes a collection of group view models and creates a matching collection of select list items.
		/// </summary>
		/// <param name="groups">The collection of group view models.</param>
		/// <param name="selected">The selected group view model, or null.</param>
		/// <returns></returns>
		public static IEnumerable<SelectListItem> ToSelectList(this IEnumerable<GroupViewModel> groups, GroupViewModel selected) {
			List<SelectListItem> output = new List<SelectListItem>();
			// Let's iterate the group collection.
			foreach (GroupViewModel group in groups) {
				// Create a select item and add it to the output.
				output.Add(new SelectListItem { Text = group.Name,
												Value = group.Id.ToString(), 
												Selected = (selected != null && group.Id == selected.Id) 
											});
			}
			return output;
		}
	}
}